package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseSystemMapper;
import com.ruoyi.base.domain.BaseSystem;
import com.ruoyi.base.service.IBaseSystemService;

import javax.annotation.PostConstruct;

/**
 * 子系统类型Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-28
 */
@Service
public class BaseSystemServiceImpl implements IBaseSystemService 
{
    public static final String CACHE_MAP = "base_system";
    private static final Logger log = LoggerFactory.getLogger(BaseSystemServiceImpl.class);

    @Autowired
    private BaseSystemMapper baseSystemMapper;
    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }


    /**
     * 查询子系统类型
     *
     * @param code 子系统代码
     * @return 子系统类型
     */
    @Override
    public BaseSystem selectBaseSystemByCode(String code){
        String key = "code_"+code;
        BaseSystem baseSystem = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(baseSystem != null){
            return baseSystem;
        }

        baseSystem = baseSystemMapper.selectBaseSystemByCode(code);
        if(baseSystem != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseSystem);
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+baseSystem.getId(), baseSystem);
        }
        return baseSystem;
    }

    /**
     * 查询子系统类型
     * 
     * @param id 子系统类型主键
     * @return 子系统类型
     */
    @Override
    public BaseSystem selectBaseSystemById(Long id)
    {
        String key = "id_"+id;
        BaseSystem baseSystem = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(baseSystem != null){
            return baseSystem;
        }

        baseSystem = baseSystemMapper.selectBaseSystemById(id);
        if(baseSystem != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseSystem);
            redisCache.setCacheMapValue(CACHE_MAP, "code_"+baseSystem.getCode(), baseSystem);
        }
        return baseSystem;
    }

    /**
     * 查询子系统类型列表
     * 
     * @param baseSystem 子系统类型
     * @return 子系统类型
     */
    @Override
    public List<BaseSystem> selectBaseSystemList(BaseSystem baseSystem)
    {
        return baseSystemMapper.selectBaseSystemList(baseSystem);
    }

    /**
     * 新增子系统类型
     * 
     * @param baseSystem 子系统类型
     * @return 结果
     */
    @Override
    public int insertBaseSystem(BaseSystem baseSystem)
    {
        BaseSystem old = selectBaseSystemByCode(baseSystem.getCode());
        if(old != null){
            throw new ServiceException("子系统已经存在: "+baseSystem.getCode());
        }
        baseSystem.setCreateTime(DateUtils.getNowDate());
        return baseSystemMapper.insertBaseSystem(baseSystem);
    }

    /**
     * 修改子系统类型
     * 
     * @param baseSystem 子系统类型
     * @return 结果
     */
    @Override
    public int updateBaseSystem(BaseSystem baseSystem)
    {
        BaseSystem old = selectBaseSystemByCode(baseSystem.getCode());
        if(old != null && !old.getId().equals(baseSystem.getId())){
            throw new ServiceException("子系统已经存在: "+baseSystem.getCode());
        }
        baseSystem.setUpdateTime(DateUtils.getNowDate());
        return baseSystemMapper.updateBaseSystem(baseSystem) + reCache();
    }

    /**
     * 批量删除子系统类型
     * 
     * @param ids 需要删除的子系统类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseSystemByIds(Long[] ids)
    {
        return baseSystemMapper.deleteBaseSystemByIds(ids) + reCache();
    }

    /**
     * 删除子系统类型信息
     * 
     * @param id 子系统类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseSystemById(Long id)
    {
        return baseSystemMapper.deleteBaseSystemById(id) + reCache();
    }


}
